package paas.storage.distributedColumnDatabase.impl;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import paas.storage.common.config.ConnectionManager;
import paas.storage.common.constant.ResponseCode;
import paas.storage.common.utils.AssertUtils;
import paas.storage.distributedColumnDatabase.IAuth;
import paas.storage.distributedColumnDatabase.IConnection;
import paas.storage.exception.RRException;
import paas.storage.result.CreateResponse;

import java.util.Map;
import java.util.UUID;

/**
 * 创建连接实现类
 *
 * @author xufeng
 * @version 1.0
 * @date 2021/1/23
 */
@Service
public class IConnectionImpl implements IConnection {

    private static Logger log = LoggerFactory.getLogger(IConnectionImpl.class);


    @Autowired
    private IAuth iAuth;
    @Autowired
    private ConnectionManager connectionManager;

    /**
     * 创建连接
     *
     * @param serviceId    服务唯一标识 必填 数据库服务地址。
     * @param accessToken  accessToken 必填 支持OAuth、LDAP等身份认证协议的TOKEN。
     * @param expendParams 扩展参数 不必填 以json字符串格式定义：
     *                     {key1:value1, key2:value2,e4。
     *                     <p>
     *                     java sdk https://github.com/yunify/qingcloud-sdk-java
     *                     <p>
     *                     token 是 ak|sk 的格式，中间用 | 分割，用上述 sdk 进行 token 校验。
     * @return
     */
    @Override
    public CreateResponse create(String serviceId, String accessToken, String expendParams) {
        //默认创建连接
        CreateResponse createResponse = new CreateResponse();
        createResponse.setTaskStatus(ResponseCode.TASK_STATUS_OK);
        try {
            AssertUtils.isTrue(accessToken == null, "accessToken不能为空");
            String[] inStrs = StringUtils.split(accessToken, "|");
            iAuth.authentication(inStrs[0], inStrs[1]);

            if (expendParams != null) {
                AssertUtils.charLengthLe(expendParams, 4000, "expendParams:扩展参数字符定长不能超过4000");
            }
            Map<String, Map<String, Object>> config = JSON.parseObject(expendParams, Map.class);
            JSON.parseObject(expendParams, Map.class);

            String uuId = UUID.randomUUID().toString();
            createResponse.setConnectionId(uuId);
            //创建连接
            connectionManager.putConnection(uuId, config);

        } catch (RRException e) {
            createResponse.setConnectionId(null);
            createResponse.setTaskStatus(ResponseCode.TASK_STATUS_ERROR);
            createResponse.setErrorCode(ResponseCode.BUSINESS_CODE);
            createResponse.setErrorMsg("业务异常," + e.getMessage());
        } catch (Exception e) {
            createResponse.setConnectionId(null);
            createResponse.setTaskStatus(ResponseCode.TASK_STATUS_ERROR);
            createResponse.setErrorCode(ResponseCode.TASK_STATUS_ERROR_CONNECTION);
            createResponse.setErrorMsg("获取连接失败," + e.getMessage());
            log.error(e.getMessage(), e);
        }

        return createResponse;
    }

}
